package Text;

import java.util.ArrayList;

public class 垃圾回收GC分析 {
    private static final int _512K=512*1024;
    private static final int _1MB=1024*1024;
    private static final int _6MB=6*1024*1024;
    private static final int _7MB=7*1024*1024;
    private static final int _8MB=8*1024*1024;
    //-Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc
    public static void main(String[] args) {
        ArrayList<byte[]> list=new ArrayList<>();
        list.add(new byte[_7MB]);
        list.add(new byte[_512K]);
        list.add(new byte[_512K]);
        //一个线程内的内存溢出不会造成整个程序的关闭
        /*list.add(new byte[_8MB]);
        list.add(new byte[_8MB]);*/
        /**list.add(new byte[_8MB]);因为新生代不够，就会直接晋升到老年代
         * Heap
         *  def new generation   total 9216K, used 2153K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
         *   eden space 8192K,  26% used [0x00000000fec00000, 0x00000000fee1a418, 0x00000000ff400000)
         *   from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
         *   to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
         *  tenured generation   total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
         *    the space 10240K,  80% used [0x00000000ff600000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000)
         *  Metaspace       used 3133K, capacity 4496K, committed 4864K, reserved 1056768K
         *   class space    used 343K, capacity 388K, committed 512K, reserved 1048576K
         * */
        /**
         * list.add(new byte[_8MB]);
         * list.add(new byte[_8MB]);两次溢出，先触发新生代垃圾回收，然后又一次进行老年代垃圾回收
         * 最后还是不够就会报错溢出
         * [GC (Allocation Failure) [DefNew: 1988K->606K(9216K), 0.0017278 secs][Tenured: 8192K->8797K(10240K), 0.0019392 secs] 10180K->8797K(19456K),
         * [Metaspace: 3183K->3183K(1056768K)], 0.0037431 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
         * [Full GC (Allocation Failure) [Tenured: 8797K->8779K(10240K), 0.0016700 secs] 8797K->8779K(19456K),
         * [Metaspace: 3183K->3183K(1056768K)], 0.0017034 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
         * Heap
         *  def new generation   total 9216K, used 410K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
         *   eden space 8192K,   5% used [0x00000000fec00000, 0x00000000fec66800, 0x00000000ff400000)
         *   from space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
         *   to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
         *  tenured generation   total 10240K, used 8779K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
         *    the space 10240K,  85% used [0x00000000ff600000, 0x00000000ffe92e98, 0x00000000ffe93000, 0x0000000100000000)
         *  Metaspace       used 3228K, capacity 4496K, committed 4864K, reserved 1056768K
         *   class space    used 349K, capacity 388K, committed 512K, reserved 1048576K
         * Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
         * 	at Text.text1.main(text1.java:18)
         * */
    }
}
